package gui;

import effect.Effect;
import effect.Effect.TargetType;
import gui.GUIConstants.SelectionStatus;


import java.util.ArrayList;
import java.util.List;

import card.BuildingCard;
import card.Card;
import card.CreatureCard;



import core.Constants;
import core.Core;
import core.Constants.ConnectionType;

/**
 *
 * @author antongr
 * Handles all selection of things.
 */
public class SelectionController {
	private static SelectionController me;
	public static SelectionController getInstance(){
		if(me == null)
			me=new SelectionController();
		return me;
	}
	private SelectionController(){

	}
	private static GUICore guiCore = GUICore.getInstance();

	/**
	 * Deselects all cards, placing the gui in a default state.
	 */
	public void deselect(){
		guiCore.setSelectedCard(null);
		guiCore.selectionSpheres.clear();
		guiCore.setSelectedCard(null);
		guiCore.setSelectedEffect(null);
		guiCore.selectionStatus=SelectionStatus.NO_SELECTION;
		EffectPopupInfo.getInstance().setName(null);
		guiCore.updateAll();
	}
	/**
	 * Selects the given tile and prepares SelectionCircles to chose Cards
	 * @param t
	 */
	public void select(GUITile t){
		List<SelectionSphere> selections = new ArrayList<SelectionSphere>();
		int i=0;
		int m=t.getTile().getAllCards().size()-1; // remove one size for landcard that always exists and is not shown
		for(Card c : t.getTile().getAllCards()){
			if(c instanceof CreatureCard || c instanceof BuildingCard){
				SelectionSphere s = new SelectionSphere(c);
				selections.add(s);
				s.setPosition(t.getPosition(), m, i);
				i++;
			}
		}
		if(!selections.isEmpty()){ //no cards on tile means no reaction needed
			guiCore.selectionSpheres=selections;
			guiCore.selectionStatus=SelectionStatus.SELECTIONSPHERE;
			guiCore.updateAll();
		}
		CardViewPanel.getInstance().setShownCard(t.getTile().getLandCard());

		
	}
	
	/**
	 * Use when selecting to use a targeted Effect
	 */
	public void select(Effect e){
		guiCore.setSelectedEffect(e);
		if(e.isTargetType(TargetType.tile))
			guiCore.selectionStatus=SelectionStatus.EXPECT_INPUT_TILE;
		if(e.isTargetType(TargetType.buildingCard))
			guiCore.selectionStatus=SelectionStatus.EXPECT_INPUT_CARD;
		if(e.isTargetType(TargetType.creatureCard))
			guiCore.selectionStatus=SelectionStatus.EXPECT_INPUT_CARD;
		if(e.isTargetType(TargetType.player)){} // TODO target player
		
	}
	
	/**
	 * Selects the given card and prepares the GUI for further actions
	 * @param t
	 */
	public void select(Card c){
		if(Constants.connectionState == ConnectionType.DISCONNECTED && c.getOwner() == Core.getInstance().getCurrentPlayer()
				|| Constants.connectionState == ConnectionType.CONNECTED && c.getOwner() == Core.getInstance().getThisPlayer()){
			guiCore.selectionStatus=SelectionStatus.CARD_SELECTED;
			guiCore.setSelectedCard(c);
			
			guiCore.updateAll();
		}
		else
			deselect();
		CardViewPanel.getInstance().setShownCard(c);
		
	}
	/**
	 * Selects the Card / ability that the sphere represents.
	 * @param s
	 */
	public void select(SelectionSphere s){
		//TODO priority 2 : work for abilities
		guiCore.selectionSpheres.clear();
		select(s.getCard());
	}
}
